/**
 * 商品基础信息编辑
 */
var oTable1;
var oTable2;
var oTable3;
$(function(){
	//初始化
	initBaseInfoDataTable();
	initProductInfoDataTable();
	initLogsDataTable();
});

function initBaseInfoDataTable() {
	oTable1 = $('#baseinfoTab').DataTable({
		"dom": '<"top">t<"bottom"><"clear">',
		"ajax": {
			"type": "POST",
			"url": "query.ajax",
			"data": function(d) {
				d.relateid = $('#relateid').val();
			}
		},
		"ordering": false,
		"iDisplayLength" : 10,
		"oLanguage": {
			"sUrl": "../static/js/datatable.cn.txt"
		},
		"aoColumns": [
		    {"mDataProp":"id"},
			{"mDataProp":"relateid"},
			{"mDataProp":"importdate"},
			{"mDataProp":"fromairport"},
			{"mDataProp":"toairport"},
			{"mDataProp":"transferairport"},
			{"mDataProp":"fromdate"},
			{"mDataProp":"todate"},
			{"mDataProp":"price"},
			{"mDataProp":"tax"},
			{"mDataProp":"total"},
			{"mDataProp":"stocks"},
			{"mDataProp":"changedstocks"},
			{"mDataProp":"takebackstocks"},
			{"mDataProp":"stockstatus"}
		],
		"columnDefs": [
		    {
		    	"targets": 0,
		    	"visible": false
		    },
		   
			{
				"targets": 5,
				"render":function(data,type,row){
					if(data==''){
						return "无";
					}
					return data;
				}
			
			}, 
			{
				"targets": 10,
				"render":function(data,type,row){
					return row.price+row.tax;
				}
			
			},
			{
				"targets": 14,
				"render":function(data,type,row){
					if(data==1){
						return "否";
					}else{
						return "是";
					}
					
				}
			},
            {
                "render": function ( data, type, row ) {
                	if(row.stockstatus==1 && row.status!=2){
                		return '<a class="btn btn-primary btn-xs" href="javascript:void(0);"  onclick="javascript:editBaseInfo('+row.relateid+')" title="编辑"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑</a>&nbsp;&nbsp;';
                	}else{
                		return '&nbsp;';
                	}
                },
                "targets": 15
            }
       ]
	});
}

function initProductInfoDataTable() {
	oTable2 = $('#productinfoTab').DataTable({
		"dom": '<"top">t<"bottom"><"clear">',
		"ajax": {
			"type": "POST",
			"url": "queryproductinfo.ajax",
			"data": function(d) {
				d.relateid = $('#relateid').val();
			}
		},
		"ordering": false,
		"iDisplayLength" : 10,
		"oLanguage": {
			"sUrl": "../static/js/datatable.cn.txt"
		},
		"aoColumns": [
		    {"mDataProp":"id"},
			{"mDataProp":"relateid"},
			{"mDataProp":"productid"},
			{"mDataProp":"flightno"},
			{"mDataProp":"fromairport"},
			{"mDataProp":"toairport"},
			{"mDataProp":"transferAirport"},
			{"mDataProp":"flighttype"},
			{"mDataProp":"price"},
			{"mDataProp":"tax"},
			{"mDataProp":"total"},
			{"mDataProp":"stocks"},
			{"mDataProp":"currentstocks"},
		],
		"columnDefs": [
		    {
		    	"targets": 0,
		    	"visible": false
		    },
			{
				"targets": 6,
				"render":function(data,type,row){
					if(data==''){
						return "无";
					}
					return data;
				}
			
			}, 
			{
				"targets": 10,
				"render":function(data,type,row){
					return row.price+row.tax;
				}
			
			},
            {
                "render": function ( data, type, row ) {
             	   return '<a class="btn btn-primary btn-xs" href="javascript:void(0);"  onclick="javascript:showProductInfo(' + row.id + ')" title="查看"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>查看</a>&nbsp;&nbsp;';
                },
                "targets": 13
            }
       ]
	});
}

function initLogsDataTable() {
	oTable3 = $('#logsTab').DataTable({
		"dom": '<"top">rt<"bottom"ip><"clear">',
		"ajax": {
			"type": "POST",
			"url": "querylogs.ajax",
			"data": function(d) {
				d.relateid = $('#relateid').val();
			}
		},
		"ordering": false,
		"processing": true,
        "serverSide": true,
		"iDisplayLength" : 10,
		"oLanguage": {
			"sUrl": "../static/js/datatable.cn.txt"
		},
		"aoColumns": [
		    {"mDataProp":"uname"},
			{"mDataProp":"context"},
			{"mDataProp":"createtime"},
		],
		"columnDefs": [
		    {
		    	"targets": 1,
		    	"render": function ( data, type, row ) {
		    		var html = "";
		    		if(row.oldprice!=row.newprice){
		    			html +="原价格："+row.oldprice+",新价格："+row.newprice+"；";
		    		}
		    		if(row.oldtax!=row.newtax){
		    			html +="原税费："+row.oldtax+",新税费："+row.newtax+"；";
		    		}
		    		if(row.oldstock!=row.newstock){
		    			html +="原库存："+row.oldstock+",新库存："+row.newstock+"；";
		    		}
		    		if(row.recycletype !=null){
		    			html +="操作回收方式："
		    			if(row.recycletype=='manual'){
		    				html +="手工回收；"
		    			}else{
		    				html +="自动回收；"
		    			}
		    		}
		    		return html;
	            },
		    },
			
       ]
	});
}